package cn.adviceclient.gui.click.neverlose.components.espelements;

import cn.adviceclient.features.module.ModuleManager;
import cn.adviceclient.features.module.impl.render.ESP2D;
import cn.adviceclient.gui.click.neverlose.NeverLose;
import cn.adviceclient.gui.click.neverlose.components.ESPPreviewComponent;
import cn.adviceclient.utility.client.math.MathUtils;
import cn.adviceclient.utility.render.RenderUtils;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import cn.adviceclient.gui.click.Component;
import java.util.ArrayList;
import java.util.List;

public class NameElement extends Component {
    private int x;
    private int y;

    @Override
    public void drawScreen(int mouseX, int mouseY) {
            x = ESPPreviewComponent.getPosX() + NeverLose.getWidth() + 120;
            y = (int) (NeverLose.espPreviewComponent.getPosY() + 35 + 75 * (1 - NeverLose.espPreviewComponent.getElementsManage().open.getOutput()));

        if (ModuleManager.getModule(ESP2D.class).tags.getValue()) {
            final FontRenderer fontRenderer = mc.fontRendererObj;
            final String name = mc.thePlayer.getDisplayName().getFormattedText() + " " + (MathUtils.roundToHalf(mc.thePlayer.getHealth())) + EnumChatFormatting.RED + "❤";

            if (ModuleManager.getModule(ESP2D.class).tagsBackground.get()) {

                RenderUtils.drawRect(
                        (x - fontRenderer.getStringWidth(name) / 2f * ModuleManager.getModule(ESP2D.class).tagsSize.getValue()),
                        (y - 2),
                        fontRenderer.getStringWidth(name) * ModuleManager.getModule(ESP2D.class).tagsSize.getValue(),
                        fontRenderer.FONT_HEIGHT * ModuleManager.getModule(ESP2D.class).tagsSize.getValue() + 1,
                        0x96000000);
            }
            fontRenderer.drawScaledString(name, (x - fontRenderer.getStringWidth(name) / 2f * ModuleManager.getModule(ESP2D.class).tagsSize.getValue()), y, ModuleManager.getModule(ESP2D.class).tagsSize.getValue(), -1);


            if (ModuleManager.getModule(ESP2D.class).item.get()) {
                List<ItemStack> items = new ArrayList<>();
                if (mc.thePlayer.getHeldItem() != null) {
                    items.add(mc.thePlayer.getHeldItem());
                }
                for (int index = 3; index >= 0; index--) {
                    ItemStack stack = mc.thePlayer.inventory.armorInventory[index];
                    if (stack != null) {
                        items.add(stack);
                    }
                }
                float armorX = x - fontRenderer.getStringWidth(name) / 2f - ((float) (items.size() * 18) / 2) * ModuleManager.getModule(ESP2D.class).tagsSize.getValue();

                for (ItemStack stack : items) {
                    RenderUtils.renderItemStack(stack, armorX, y - 25 * ModuleManager.getModule(ESP2D.class).tagsSize.getValue(), ModuleManager.getModule(ESP2D.class).tagsSize.getValue() + ModuleManager.getModule(ESP2D.class).tagsSize.getValue() / 2, true);
                    armorX += 18 * ModuleManager.getModule(ESP2D.class).tagsSize.getValue();
                }
            }
        }
        super.drawScreen(mouseX, mouseY);
    }

    @Override
    public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        super.mouseClicked(mouseX, mouseY, mouseButton);
    }

    @Override
    public void mouseReleased(int mouseX, int mouseY, int state) {
        super.mouseReleased(mouseX, mouseY, state);
    }

    @Override
    public void keyTyped(char typedChar, int keyCode) {
        super.keyTyped(typedChar, keyCode);
    }
}